﻿using System.Windows;
using System.Windows.Input;
using Microsoft.Phone.Shell;
using Samples.Radio.Agent;
using WindowsPhoneAudioStreaming.Audio.Streaming;

namespace Samples.Radio.Pages.Main
{
    public partial class MainPage 
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            (DataContext as MainPageViewModel).BufferStatusChanged += BufferStatusChanged;
        }

        private void BufferStatusChanged(object sender, DualParameterEventArgs<BufferStatus, double> e)
        {
            var progress = SystemTray.ProgressIndicator;
            if(progress==null)
            {
                progress = new ProgressIndicator
                               {
                                   IsVisible = false,
                                   Text = e.Parameter1==BufferStatus.Opening?"Opening...": "Buffering..."
                               };
                SystemTray.ProgressIndicator = progress;
            }

            progress.IsVisible = e.Parameter1!=BufferStatus.NotBuffering;
            progress.Value = e.Parameter2;
        }

        private void FeaturedProgramTap(object sender, GestureEventArgs e)
        {
            var program = (sender as FrameworkElement).DataContext as ProgramViewModel;
            (DataContext as MainPageViewModel).DisplayFeaturedProgram(program);
        }
    }
}